package be.dch.gui.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import org.apache.log4j.Logger;

import be.dch.gui.components.JTextFieldBrowse;
import be.dch.splitter.SplitterConfig;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public final class SplitterPanel extends AbstractPanel implements ActionListener {
    private static final Logger LOG = Logger.getLogger(SplitterPanel.class);
    private static final long serialVersionUID = -5758797710302854440L;
    private static final String[] OUTPUT_TYPES = { "xml", "pbf" };
    private static final String COLUMN_SPECS = "1dlu:grow,50dlu,3dlu,150dlu,3dlu,50dlu,3dlu,50dlu,1dlu:grow";
    private static final String ROW_SPECS = "pref,3dlu,15dlu,3dlu,15dlu,3dlu,15dlu,3dlu,15dlu,3dlu,15dlu,3dlu,15dlu,3dlu,pref,3dlu,pref,3dlu,pref,3dlu,pref,3dlu,pref,3dlu,pref,3dlu,pref,3dlu,pref,3dlu,pref:grow";

    private static SplitterPanel instance = new SplitterPanel();

    private final transient JTextFieldBrowse txtOutputDir;
    private final transient SplitterConfig config = new SplitterConfig();

    private SplitterPanel() {
	super();
	final FormLayout layout = new FormLayout(SplitterPanel.COLUMN_SPECS, SplitterPanel.ROW_SPECS);
	final CellConstraints constraints = new CellConstraints();
	final PanelBuilder builder = new PanelBuilder(layout);

	final JLabel lblDescription = new JLabel("Description:");
	builder.add(lblDescription, constraints.rchw(3, 2, 1, 1));

	final JTextField txtDescription = new JTextField(this.config.getDescription());
	lblDescription.setLabelFor(txtDescription);
	builder.add(txtDescription, constraints.rchw(3, 4, 1, 1));

	final JLabel lblMapId = new JLabel("MapId:");
	builder.add(lblMapId, constraints.rchw(5, 2, 1, 1));

	final JTextField txtMapId = new JTextField(this.config.getMapId());
	lblMapId.setLabelFor(txtMapId);
	builder.add(txtMapId, constraints.rchw(5, 4, 1, 1));

	this.txtOutputDir = new JTextFieldBrowse("Output dir:", this, JFileChooser.DIRECTORIES_ONLY);
	builder.add(this.txtOutputDir.getLabel(), constraints.rchw(7, 2, 1, 1));
	builder.add(this.txtOutputDir.getTextField(), constraints.rchw(7, 4, 1, 1));
	builder.add(this.txtOutputDir.getButton(), constraints.rchw(7, 6, 1, 1));

	final JLabel lblMaxAreas = new JLabel("Max Areas:");
	builder.add(lblMaxAreas, constraints.rchw(9, 2, 1, 1));

	final SpinnerModel mdlMaxAreas = new SpinnerNumberModel((int) this.config.getMaxAreas(), 1, 2048, 1);
	final JSpinner jspMaxAreas = new JSpinner(mdlMaxAreas);
	lblMaxAreas.setLabelFor(jspMaxAreas);
	builder.add(jspMaxAreas, constraints.rchw(9, 4, 1, 1));

	final JLabel lblMaxNodes = new JLabel("Max Nodes:");
	builder.add(lblMaxNodes, constraints.rchw(11, 2, 1, 1));

	final SpinnerModel mdlMaxNodes = new SpinnerNumberModel(1600000, 1, 1879600000, 1);
	final JSpinner jspMaxNodes = new JSpinner(mdlMaxNodes);
	lblMaxNodes.setLabelFor(jspMaxNodes);
	builder.add(jspMaxNodes, constraints.rchw(11, 4, 1, 1));

	final JLabel lblMaxThreads = new JLabel("Max Threads:");
	builder.add(lblMaxThreads, constraints.rchw(13, 2, 1, 1));

	final SpinnerModel mdlMaxThreads = new SpinnerNumberModel(15, 1, 64, 1);
	final JSpinner jspMaxThreads = new JSpinner(mdlMaxThreads);
	lblMaxThreads.setLabelFor(jspMaxThreads);
	jspMaxThreads.setEnabled(false);
	builder.add(jspMaxThreads, constraints.rchw(13, 4, 1, 1));

	final JCheckBox jcbAutoThreads = new JCheckBox();
	jcbAutoThreads.setSelected(true);
	builder.add(jcbAutoThreads, constraints.rchw(13, 6, 1, 1));

	final JLabel lblMixed = new JLabel("Mixed:");
	builder.add(lblMixed, constraints.rchw(15, 2, 1, 1));

	final JCheckBox jcbMixed = new JCheckBox();
	jcbMixed.setSelected(true);
	lblMixed.setLabelFor(jcbMixed);
	builder.add(jcbMixed, constraints.rchw(15, 4, 1, 1));

	final JLabel lblNoTrim = new JLabel("No Trim:");
	builder.add(lblNoTrim, constraints.rchw(17, 2, 1, 1));

	final JCheckBox jcbNoTrim = new JCheckBox();
	jcbMixed.setSelected(true);
	lblNoTrim.setLabelFor(jcbNoTrim);
	builder.add(jcbNoTrim, constraints.rchw(17, 4, 1, 1));

	final JLabel lblOutputType = new JLabel("Output Type");
	builder.add(lblOutputType, constraints.rchw(19, 2, 1, 1));

	final JComboBox comOutputType = new JComboBox(SplitterPanel.OUTPUT_TYPES);
	comOutputType.setSelectedIndex(1);
	lblOutputType.setLabelFor(comOutputType);
	builder.add(comOutputType, constraints.rchw(19, 4, 1, 1));

	final JTextFieldBrowse txtGeoFilesName = new JTextFieldBrowse("Geonames file:", this, JFileChooser.FILES_ONLY);
	builder.add(txtGeoFilesName.getLabel(), constraints.rchw(21, 2, 1, 1));
	builder.add(txtGeoFilesName.getTextField(), constraints.rchw(21, 4, 1, 1));
	builder.add(txtGeoFilesName.getButton(), constraints.rchw(21, 6, 1, 1));

	final JLabel lblOverlap = new JLabel("Overlap");
	builder.add(lblOverlap, constraints.rchw(23, 2, 1, 1));

	final SpinnerModel mdlOverlap = new SpinnerNumberModel(2000, 1, 8000, 1);
	final JSpinner jspOverlap = new JSpinner(mdlOverlap);
	lblOverlap.setLabelFor(jspOverlap);
	builder.add(jspOverlap, constraints.rchw(23, 4, 1, 1));

	final JLabel lblResolution = new JLabel("Resolution:");
	builder.add(lblResolution, constraints.rchw(25, 2, 1, 1));

	final SpinnerModel mdlResolution = new SpinnerNumberModel(13, 1, 25, 1);
	final JSpinner jspResolution = new JSpinner(mdlResolution);
	lblResolution.setLabelFor(jspResolution);
	builder.add(jspResolution, constraints.rchw(25, 4, 1, 1));

	final JTextFieldBrowse txtSplitFile = new JTextFieldBrowse("Split file:", this, JFileChooser.DIRECTORIES_ONLY);
	builder.add(txtSplitFile.getLabel(), constraints.rchw(27, 2, 1, 1));
	builder.add(txtSplitFile.getTextField(), constraints.rchw(27, 4, 1, 1));
	builder.add(txtSplitFile.getButton(), constraints.rchw(27, 6, 1, 1));

	final JLabel lblStatusFreq = new JLabel("Status freq:");
	builder.add(lblStatusFreq, constraints.rchw(29, 2, 1, 1));

	final SpinnerModel mdlStatusFreq = new SpinnerNumberModel(120, 0, 300, 1);
	final JSpinner jspStatusFreq = new JSpinner(mdlStatusFreq);
	lblStatusFreq.setLabelFor(jspStatusFreq);
	builder.add(jspStatusFreq, constraints.rchw(29, 4, 1, 1));

	final JLabel lblWriteKml = new JLabel("Write kml:");
	builder.add(lblWriteKml, constraints.rchw(31, 2, 1, 1));

	final JTextField txtWriteKml = new JTextField();
	lblWriteKml.setLabelFor(txtWriteKml);
	builder.add(txtWriteKml, constraints.rchw(31, 4, 1, 1));

	this.add(builder.getPanel());

    }

    public static SplitterPanel getInstance() {

	return SplitterPanel.instance;
    }

    @Override
    public String getPanelTitle() {
	return "Splitter";
    }

    @Override
    public Icon getPanelIcon() {
	return this.createImageIcon("/images/file-splitter-icon.gif", "Splitter");
    }

    public void actionPerformed(final ActionEvent event) {
	SplitterPanel.LOG.info(event.getActionCommand());

    }

}
